package com.rongshu.view.discovery.activity;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.eventbus.DiscoverySearchResult;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.base.BaseFragmentListener;
import com.rongshu.view.discovery.adapter.SearchResultPageAdapter;
import com.rongshu.view.home.bean.LabelResponse;
import com.rongshu.widget.TabPageIndicator;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * created by zhengweis on 2018/11/1.
 * description：推荐-搜索页面
 */
public class SearchResultActivity extends BaseActivity {


    @BindView(R.id.search_left_tv)
    public EditText searchLeftTv;
    @BindView(R.id.indicator)
    TabPageIndicator indicator;
    @BindView(R.id.viewPager)
    ViewPager viewPager;

    private SearchResultPageAdapter adapter;

    @Override
    protected int initLayout() {
        return R.layout.activity_search_result;
    }

    @Override
    protected void initView() {
        adapter = new SearchResultPageAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(3);
        indicator.setViewPager(viewPager);
        adapter.setListener(new BaseFragmentListener() {
            @Override
            public void onCallBack(Object object) {
                if (object instanceof Integer) {
                    int type = (int) object;
                    switch (type) {
                        case SearchResultPageAdapter.MORE_ARTICLE:
                            viewPager.setCurrentItem(1);
                            break;
                        case SearchResultPageAdapter.MORE_AUTHOR:
                            viewPager.setCurrentItem(2);
                            break;
                    }
                }
            }
        });
        setTabPagerIndicator();
        searchLeftTv.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    String msg = v.getText().toString();
                    DiscoverySearchResult result = new DiscoverySearchResult();
                    result.setMsg(msg);
                    EventBus.getDefault().post(result);
                    showKeyboard(false);
                }
                return false;
            }
        });
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        LabelResponse.ResponseBean bean = (LabelResponse.ResponseBean) intent.getSerializableExtra("searchMsg");
        if (bean != null) {
            searchLeftTv.setText(bean.getTitle());
        }
        adapter.setData(bean);
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    private void setTabPagerIndicator() {
        indicator.setIndicatorMode(TabPageIndicator.IndicatorMode.MODE_WEIGHT_NOEXPAND_SAME);// 设置模式，一定要先设置模式
        indicator.setIndicatorColor(ContextCompat.getColor(this, R.color.color_3CC051));// 设置底部导航线的颜色
        indicator.setTextColor(ContextCompat.getColor(this, R.color.color_2c2c2c));
        indicator.setTextColorSelected(ContextCompat.getColor(this, R.color.color_3CC051));// 设置tab标题选中的颜色
        indicator.setTextSize((int) getResources().getDimension(R.dimen.dd_dimen_30px));// 设置字体大小
    }

    @OnClick({R.id.search_right_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.search_right_tv:
                showKeyboard(false);
                finish();
                break;
        }
    }
}
